/**
 * Created by meiguizhinian on 2018/6/22
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

/**
 * web版没有搜索路径，应为这样太耗时间了，所有文件在场景进入之前都要预加载
 * 所以对应的文件都在预加载都缓存里，我们可以从预加载都缓存里查找文件是否存在。
 */
cc.assert(!cc.FileUtils);
cc.FileUtils = {
	getInstance : function() {
		return this;
	},
};
if (cc.sys.isNative) {
	cc.FileUtils.addSearchPath = function(path) {
		jsb.fileUtils.addSearchPath(path);
	};

	//根目录是res
	cc.FileUtils.fullPathForFilename = function(path) {
		return jsb.fileUtils.fullPathForFilename(path);
	};

	cc.FileUtils.isFileExist = function(path) {
		return jsb.fileUtils.isFileExist(path);
	};

	cc.FileUtils.getStringFromFile = function(path) {
		let str = jsb.fileUtils.getStringFromFile(path);
		if (!str) {
			cc.log("未找到文件：%s",path);
		}
		return str;
	};
	cc.FileUtils.getDataFromFile = function(path) {
		let data = jsb.fileUtils.getDataFromFile(path);
		if (!data) {
			cc.log("未找到文件：%s",path);
		}
		return data;
	};
	cc.FileUtils.getRes = function(path) {
		return cc.loader.getRes(path);
	};

	cc.FileUtils.getValueMapFromFile = function(path) {
		return jsb.fileUtils.getValueMapFromFile(path);
	};
	cc.FileUtils.getInstance().addSearchPath("res/");
} else {
	cc.FileUtils.addSearchPath = function(path) {
		this._searchPaths.push(path);
	};

	//根目录是res
	cc.FileUtils.fullPathForFilename = function(path) {
		let fullpath = cc.FileUtils.__fullPathForFilename(path);
		if (!fullpath) {
			cc.log("未找到文件：%s",path);
		}
		return fullpath;
	};

	cc.FileUtils.isFileExist = function(path) {
		return !!this.fullPathForFilename(path);
	};

	cc.FileUtils.getStringFromFile = function(path) {
		let fullpath = this.fullPathForFilename(path);
		if (fullpath) {
			return cc.loader.__getRes(fullpath);
		}
		return null;
	};
	cc.FileUtils.getRes = function(path) {
		let fullpath = this.fullPathForFilename(path);
		if (fullpath) {
			return cc.loader.__getRes(fullpath);
		}
		return null;
	};
	cc.FileUtils.getDataFromFile = function(path) {
		let fullpath = this.fullPathForFilename(path);
		if (fullpath) {
			return cc.loader.__getRes(fullpath);
		}
		return null;
	};

	cc.FileUtils.getValueMapFromFile = function(path) {
		let data = this.getDataFromFile(path);
		if (data) {
			//TODO
		}
		return null;
	};
	////////////////////////////////////////////////////////////////////////////
	cc.FileUtils.__fullPathForFilename = function(path) {
		for (let i=this._searchPaths.length-1; i!==0; --i) {
			let fullpath = this._searchPaths[i]+"/"+path;
			let ret = cc.loader.__getRes(fullpath);
			if (ret) {
				return fullpath;
			}
		}
		let ret = cc.loader.__getRes(path);
		if (ret) {
			return path;
		}
		return null;
	};
	//////////从后往前搜索
	cc.FileUtils._searchPaths = ["","res"];
	////////////////////////////////////////////////////////////////////////
	cc.textureCache.__getTextureForKey = cc.textureCache.getTextureForKey;
	cc.textureCache.getTextureForKey = function (textureKeyName) {
		let textureKeyName_ = cc.FileUtils.__fullPathForFilename(textureKeyName);
		if (!textureKeyName_) {
			textureKeyName_ = textureKeyName;
		}
		return cc.textureCache.__getTextureForKey(textureKeyName_);
	};

	cc.loader.__getRes = cc.loader.getRes;
	cc.loader.getRes = function (url) {
		let url_ = cc.FileUtils.__fullPathForFilename(url);
		if (!url_) {
			url_ = url;
		}
		return cc.loader.__getRes(url_);
	}
}
